<script>
var last_id ='';
$(document).ready(function(){
	$("#selForm").ajaxForm({
    	dataType:  'json',
    	success: showResponse
    });

	function showResponse(res) {
		if (res.error) {
			var message = '';
			$.each(res.error,function(i,val){
				$.each(val,function(ii,vv){
					$("#"+i+"-"+ii).addClass('ui-state-error');
					$.each(vv,function(iii,vvv){
						message += $("#"+ii+"-label label").html()+': '+ vvv+ "\n";
					});
				});
			});
			jAlert(message, '<?php echo $this->translate('checking errors')?>');

		}
		else{
			$(".ui-state-error").removeClass('ui-state-error ');
			$("#selForm input:text").val('').attr('disabled','');
			$("#saveBtn").attr('disabled','true');
			getallData();
		}
		$('#main_body').unblock();
	}

	function getallData() {
		$("#student_id").attr('value','');
		$("#year-semester-span").html($("#year_semester option:selected").text());
		var year_semester = $("#year_semester").val();
		//$("#data-section").hide();
		$.get('<?php echo $this->route('getclass')?>',{year_semester:year_semester},function(data){
			ss = '<option value=""><?php echo $this->translate('choose a class')?></option>'+data;
			$("#class_id").html(ss);
		});
		$.get('<?php echo $this->route('get-student')?>',{year_semester:year_semester, kind:'in'},function(data){
			$("#student-list").html(data);
		});
	}

	$("#saveBtn").click(function(){
		$('#main_body').block({message:'<h1><?php echo $this->translate('Data setting ..')?></h1>',  border: '3px solid #f00'});
		$("#selForm").submit();
	});


	$("#year_semester").change(function(){
		$("#selForm input:text").val('');
		$("#saveBtn").attr('disabled','disabled');
		getallData();
	});

	$("#student_id").change(function(){
		var student_id =$(this).val();
		var year_semester = $("#year_semester").val();
		$.getJSON('<?php echo $this->route('queryid')?>',{year_semester:year_semester,student_id:student_id},function(data){
			if (data){
				alert(' <?php echo $this->translate("Can't double student_id")?>: '+$("#student_id").val()+'--'+data);
				$("#student_id").val(last_id);
			}
		});
	});

	$("#class_id").change(function(){
		$(".ui-state-error").removeClass('ui-state-error ');
		$("#saveBtn").attr('disabled','disabled');
		if ($(this).val()!='') {
			var grade = $(this).val();
			var year_semester = $("#year_semester").val();
			$.getJSON('<?php echo $this->route('getlastid')?>',{grade:grade,year_semester:year_semester},function(res){
				$("#move_in-student_id").attr('value',res.student_id);
				$("#move_in-number").attr('value',res.number);
				last_id = res.student_id;
			});
			$("#saveBtn").attr('disabled','');
		}
	});

	$("#cancelBtn").click(function(){

		$("#selForm").attr('action','<?php echo $this->route('movein-set')?>');
		$("#selForm input:text").val('').attr('disabled','');
		$("#selForm input:hidden").val('');
		$("#class_id").attr('disabled','').val('');
		$(".ui-state-error").removeClass('ui-state-error ');
		$("#saveBtn").attr('disabled','disabled');

	});

	$("#zoom-in").click(function(){
		$.openDOMWindow({
	        loader:1,
	        loaderImagePath:'animationProcessing.gif',
	        loaderHeight:16,
	        loaderWidth:17,
	        width : 1009,
	        draggable:1,
	        windowSourceID:'#student-list'
	    });
	});
	getallData();
});

</script>
<style>
#data-section {margin: 5px 0; padding:5px;border:thin solid #ccc;}
#data-section div {margin:0 5px;}
#student-list {overflow: auto;height:350px;width:480px;background:#ffe;border:thin solid #ccc;padding:3px;}
#student-list table {white-space: nowrap;}
.tablesorter {cursor: pointer}
.trover {background-color: #ffccCC;}
.trclick {background-color: #DDffCC;}

</style>
<div id="main_body">
<form action="<?php echo $this->route('movein-set')?>"  method="post" id="selForm" >
<div id="select-section">
<select name="year_semester" id="year_semester">
<?php echo $this->YearSemesterOptions($this->year_semester)?>
</select>
<label for="class_id"><?php echo $this->translate('transfer to class')?></label>
<select name="class_id" id="class_id">
</select>

<input type="button"  id="saveBtn" value="<?php echo $this->translate('save')?>"  disabled="disabled" style="margin:0 8px;"/>
<input type="button"  id="cancelBtn" value="<?php echo $this->translate('cancel')?>"  style="margin:0 8px;"/>
</div>

<div id="data-section" >
<div id="left-data" style="float:left">
<?php echo $this->form->student_id?>
<?php echo $this->form->number?>
<?php echo $this->form->name?>
<?php echo $this->form->sex?>
<?php echo $this->form->identification_number?>
<?php echo $this->form->county?>
<?php echo $this->form->school_name?>
<?php echo $this->form->date?>
</div>
<div id="right-data" style="float:left">

<?php echo $this->form->school_id?>
<?php echo $this->form->approve_organ?>
<?php echo $this->form->causation?>
<?php echo $this->form->approve_date?>
<?php echo $this->form->approve_word?>
<?php echo $this->form->approve_number?>
</div>
<div id="list-data" style="float:left">
<h2><span id="year-semester-span"></span> <?php echo $this->translate('all transfer in data')?>
<img  id="zoom-in" src="<?php echo $this->baseUrl?>/images/zoom-in.png"  style="margin-left:5px" />
</h2>
<div id="student-list"></div>
</div>

<div style="clear:both"></div>
</div>
<input type="hidden" id="move_in-sn" name="sn" />
</form>
</div>